home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / misc.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  16KB  |  573 lines

  1. /* misc.c  miscellaneous commands, some macros and mode commands */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <osbind.h>
  6. #include "ed.h"
  7.  
  8. typedef short boolean;
  9. static int count = 1;
  10. char *modes[] = {"Fundamental","Wrap","C"};
  11.  
  12. /* MINDNL : META command Indent subsequent newline same as the present
  13.  * one.  Bound to M-J.
  14.  */
  15.  
  16. mindnl(f, n)
  17. register int f, n;
  18. {
  19.         curwp->w_doto = llength(curwp->w_dotp);
  20.         return(indent(f, n));
  21. }
  22.  
  23. /* MDELELN : CTRL command Delete entire line from beginning. Bound to ^K.
  24.  */
  25.  
  26. mdeleln(f, n)
  27. register int f, n;
  28. {
  29.         curwp->w_doto = 0;
  30.         return(kill(NULL, 1));
  31. }
  32.  
  33. /* MDELWLN : META command Delete entire line including NL. Bound to M-^K.
  34.  */
  35.  
  36. mdelwln(f, n)
  37. register int f, n;
  38. {
  39.         curwp->w_doto = 0;
  40.         return(kill(TRUE, 1));
  41. }
  42.  
  43. /* MDELIND : META command Delete beginning indentation on line. Bound to M-\.
  44.  */
  45.  
  46. mdelind(f, n)
  47. register f, n;
  48. {
  49.         register int odo;
  50.  
  51.         f = llength(curwp->w_dotp);
  52.         odo = curwp->w_doto;
  53.  
  54.         curwp->w_doto = 0;
  55.         while ((n = lgetc(curwp->w_dotp, curwp->w_doto)) <= ' '
  56.                 && curwp->w_doto != llength(curwp->w_dotp))
  57.                 if (forwdel(NULL, 1) == FALSE)
  58.                         return(FALSE);
  59.         if ((n = llength(curwp->w_dotp)) == NULL)
  60.                 curwp->w_doto = n;
  61.         else {
  62.                 if (odo == NULL)
  63.                         curwp->w_doto = odo;
  64.                 else
  65.                         curwp->w_doto = odo - (f - n);
  66.         }
  67.         lchange(WFEDIT);
  68.         return(TRUE);
  69. }
  70.  
  71. /* CLOWSP Meta Command  Close up white space from point forward.
  72.  * Bound to M-^O.
  73.  */
  74.  
  75. clowsp(f, n)
  76. register int f, n;
  77. {
  78.         while ((n = lgetc(curwp->w_dotp, curwp->w_doto)) <= ' '
  79.                 || curwp->w_doto == llength(curwp->w_dotp))
  80.                 if(forwdel(NULL, 1) == FALSE)
  81.                         return(FALSE);
  82.         return(linsert(1, ' '));
  83. }
  84.  
  85. /* MARKPAR : Meta Command Mark paragraph for moving or deleting.  Bound to
  86.  * M-H.
  87.  */
  88.  
  89. markpar(f, n)
  90. register int f, n;
  91. {
  92.         goteop(NULL, n);
  93.         setmark(NULL, 1);
  94.         gotbop(NULL, n);
  95.         return(TRUE);
  96. }
  97.  
  98. /* KILLSENT : Meta command  Kills sentence forward.  Bound to M-K.
  99.  */
  100.  
  101. killsent(f, n)
  102. register int f, n;
  103. {
  104.         setmark(NULL,1);
  105.         forwsent(f, n);
  106.         return(killregion(NULL, 1));
  107.  
  108. }
  109.  
  110. /* TGLCASE : Meta command  Toggles case of letter at point.  Bound to M-^P.
  111.  */
  112.  
  113. tglcase(f, n)
  114. register int f, n;
  115. {
  116.         register int c;
  117.  
  118.         if (n < 0)
  119.                 return(FALSE);
  120.         while(n--)
  121.                 {
  122.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  123.                 if(islower(c))
  124.                         c = toupper(c);
  125.                 else
  126.                         c = tolower(c);
  127.  
  128.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  129.                 lchange(WFEDIT);
  130.                 if(forwchar(FALSE, 1) == FALSE)
  131.                         return(FALSE);
  132.                 }
  133.         return(TRUE);
  134. }
  135.  
  136. /* GRTW eXtended command  Globally removes trailing white space.  Bound to
  137.  * ^X-\.
  138.  */
  139.  
  140. grtw(f, n)
  141. register int f, n;
  142. {
  143.  
  144.         register int dlo, d;
  145.         register LINE *dlp;
  146.  
  147.         dlp = curwp->w_dotp;
  148.         dlo = curwp->w_doto;
  149.  
  150.         gotobob(NULL, 1);
  151.  
  152.         while((d = ltrw(FALSE, 1)) != EOF)
  153.                 forwline(NULL, 1);
  154.  
  155.         if (dlo > llength(dlp))
  156.                 dlo = llength(dlp);
  157.  
  158.         curwp->w_dotp = dlp;
  159.         curwp->w_doto = dlo;
  160.         curwp->w_flag |= WFHARD;
  161.         return(TRUE);
  162. }
  163.  
  164. /* LTRW internal function delete trailing white space on current line.
  165.  * called by grtw() and fillpar().  Any arguments are ignored.  Returns
  166.  * length of line or EOF at end of buffer.
  167.  */
  168.  
  169. ltrw(f, n)
  170. register int f, n;
  171. {
  172.         register int c, d;
  173.  
  174.         if(curwp->w_dotp == curbp->b_linep)
  175.                 return(EOF);
  176.         d = llength(curwp->w_dotp);
  177.         curwp->w_doto = d;
  178.  
  179.         if (d == NULL)
  180.                 return(NULL);
  181.         while (--d >= 0)
  182.                 {
  183.                 backchar(FALSE, 1);
  184.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  185.                 if(c != ' ' && c != '\t')
  186.                         break;
  187.                 if(ldelete(1, NULL) == FALSE)
  188.                         return(EOF);
  189.                 }
  190.         return(llength(curwp->w_dotp));
  191. }
  192.  
  193. /* TWADDLE Meta command  More at twiddle.  Transpose two words on a line.
  194.  * Bound to M-T.
  195.  */
  196.  
  197. twaddle(f, n)
  198. register int f, n;
  199. {
  200.         boolean flag = FALSE;
  201.         boolean punct= FALSE;
  202.         register int c;
  203.         char word[20];
  204.  
  205.         n = 0;
  206.  
  207.         if(backchar(FALSE, 1) == FALSE)
  208.                 return (FALSE);
  209.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  210.         if(ispunct(c))
  211.                 {
  212.                 punct = c;
  213.                 if (forwdel(NULL, 1) == FALSE)
  214.                         return (FALSE);
  215.                 }
  216.         else if (forwchar(FALSE, 1) == FALSE)
  217.                         return (FALSE);
  218.  
  219.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  220.         if (isspace(c))
  221.                 flag = c;
  222.         else if (ispunct(c))
  223.                 flag = c;
  224.         else
  225.                 word[n++] = c;
  226.         if (ldelete(1, NULL) == FALSE)
  227.                 return (FALSE);
  228.  
  229.         while(c = lgetc(curwp->w_dotp, curwp->w_doto))
  230.                 {
  231.                 if (curwp->w_doto == llength(curwp->w_dotp))
  232.                         break;
  233.                 if (isalnum(c))
  234.                         {
  235.                         word[n++] = c;
  236.                         if (forwdel(NULL, 1) == FALSE)
  237.                                 return (FALSE);
  238.                         }
  239.                 else
  240.                         break;
  241.                 }
  242.         if(backword(NULL, 1) == FALSE)
  243.                 return (FALSE);
  244.         c = 0;
  245.         while(n--)
  246.                 if (linsert(1, word[c++]) == FALSE)
  247.                         return (FALSE);
  248.         if (punct)
  249.                 if (linsert(1, punct) == FALSE)
  250.                         return (FALSE);
  251.         if (flag)
  252.                 if (linsert(1, flag) == FALSE)
  253.                         return (FALSE);
  254.         return(TRUE);
  255. }
  256.  
  257. /* MRFLUSH META Command right flush current line.  Bound to M-^R */
  258.  
  259. mrflush(f, n)
  260. register int f, n;
  261. {
  262.         register short p;       /* where we are in line */
  263.         register short l;       /* length of line */
  264.         register short o;       /* offset for new location */
  265.  
  266.         p = curwp->w_doto;
  267.         if((l = llength(curwp->w_dotp)) == NULL)
  268.                 return(FALSE);
  269.         if (fillcol)
  270.                 f = fillcol;
  271.         else    f = 80;
  272.         if((o = f - l) < 0)
  273.                 return(FALSE);
  274.         curwp->w_doto = 0;
  275.         linsert(o, ' ');
  276.         forwchar(NULL, p);
  277.         return(TRUE);
  278. }
  279.  
  280. /* MCENTER META Command center current line.  Bound to M-^C */
  281.  
  282. mcenter(f, n)
  283. register int f, n;
  284. {
  285.         register short p;       /* where we are in line */
  286.         register short l;       /* length of line */
  287.         register short o;       /* offset for new location */
  288.  
  289.         p = curwp->w_doto;
  290.         if((l = llength(curwp->w_dotp)) == NULL)
  291.                 return(FALSE);
  292.         if (fillcol)
  293.                 f = fillcol;
  294.         else    f = 80;
  295.         if((o = (f - l)/2) < 0)
  296.                 return(FALSE);
  297.         curwp->w_doto = 0;
  298.         linsert(o, ' ');
  299.         forwchar(NULL, p);
  300.         return(TRUE);
  301. }
  302.  
  303. /* SETMODE Extended Command.  Only one mode at a time.  Bound to ^X-M.
  304.  */
  305.  
  306. setmode(f, n)
  307. register int f, n;
  308. {
  309.         char mymode[12];
  310.         register char *ptr;
  311.         extern char *index();
  312.         register BUFFER *bp;
  313.         register WINDOW *wp;
  314.  
  315.         if((f=mlreply("New mode: ",mymode,12)) != TRUE)
  316.                 return(f);
  317.         /* don't fail because of stray spaces */
  318.         if ((ptr=index(mymode,' '))!=NULL)
  319.                 *ptr = '\0';
  320.         else if ((ptr=index(mymode,'\t'))!=NULL)
  321.                 *ptr = '\0';
  322.         n = strlen(mymode);
  323.         /* adjust case */
  324.         for (f=0;f<=n;f++)
  325.                 if (f == 0)
  326.                         mymode[f] = toupper(mymode[f]);
  327.                 else
  328.                         mymode[f] = tolower(mymode[f]);
  329.         for (